//
//  DataStoreService.m
//  QuXiang
//
//  Created by Duncan on 2020/6/22.
//  Copyright © 2020 NanHuaiZi. All rights reserved.
//

#import "DataStoreService.h"
#import <YYModel/YYModel.h>
#import <YTKKeyValueStore.h>

static NSString *const kDatabaseName = @"ts.db";   // 数据库

@interface DataStoreService ()

@property (nonatomic, strong) YTKKeyValueStore *store;

@end

@implementation DataStoreService

+ (instancetype)shareInstance {
    static DataStoreService *instance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[DataStoreService alloc] init];
        instance.store = [[YTKKeyValueStore alloc] initDBWithName:kDatabaseName];
    });
    return instance;
}

//初始化数据库
+ (YTKKeyValueStore *)createStore:(DataStoreTableType)tableName {
    YTKKeyValueStore *store = [DataStoreService shareInstance].store;
    [store createTableWithName:[DataStoreService getTableName:tableName]];
    return store;
}

+ (NSString *)getTableName:(DataStoreTableType)tableName {
    NSDictionary *dic = @{@(User)     : @"user_table",
                          @(Device)     : @"device_table",};
    return dic[@(tableName)];
}

+ (void)putObject:(id)object withId:(NSString *)objectId intoTable:(DataStoreTableType)tableName {
    [[DataStoreService createStore:tableName] putObject:object withId:objectId intoTable:[DataStoreService getTableName:tableName]];
}

+ (void)putString:(NSString *)string withId:(NSString *)stringId intoTable:(DataStoreTableType)tableName {
    [[DataStoreService createStore:tableName] putString:string withId:stringId intoTable:[DataStoreService getTableName:tableName]];
}

+ (void)putNumber:(NSNumber *)number withId:(NSString *)numberId intoTable:(DataStoreTableType)tableName {
    [[DataStoreService createStore:tableName] putNumber:number withId:numberId intoTable:[DataStoreService getTableName:tableName]];
}

+ (void)putModel:(id)model withId:(NSString *)modelId intoTable:(DataStoreTableType)tableName {
    NSDictionary *json = [model yy_modelToJSONObject];
    [self putObject:json withId:modelId intoTable:tableName];
}

#pragma mark 取

+ (NSArray *)getAllModelFromTable:(DataStoreTableType)tableName {
    return [[DataStoreService createStore:tableName] getAllItemsFromTable:[DataStoreService getTableName:tableName]];
}

+ (NSArray *)getAllModelFromTable:(DataStoreTableType)tableName className:(Class)className {
    NSArray *resultList = [self getAllModelFromTable:tableName];
    NSMutableArray *handleList = [NSMutableArray array];
    for (YTKKeyValueItem *item in resultList) {
        [handleList addObject:[className yy_modelWithDictionary:item.itemObject]];
    }
    return [handleList mutableCopy];
}

+ (id)getModelById:(NSString *)modelId className:(Class)className fromTable:(DataStoreTableType)tableName {
    id json = [self getObjectById:modelId fromTable:tableName];
    return [className yy_modelWithJSON:json];
}

+ (NSString *)getStringById:(NSString *)stringId fromTable:(DataStoreTableType)tableName {
    return [[DataStoreService createStore:tableName] getStringById:stringId fromTable:[DataStoreService getTableName:tableName]];
}

+ (NSNumber *)getNumberById:(NSString *)numberId fromTable:(DataStoreTableType)tableName {
    return [[DataStoreService createStore:tableName] getNumberById:numberId fromTable:[DataStoreService getTableName:tableName]];
}

+(id)getObjectById:(NSString *)objectId fromTable:(DataStoreTableType)tableName {
    return [[DataStoreService createStore:tableName] getObjectById:objectId fromTable:[DataStoreService getTableName:tableName]];
}

#pragma mark 删

+ (void)deleteObjectById:(NSString *)objectId fromTable:(DataStoreTableType)tableName {
    [[DataStoreService createStore:tableName] deleteObjectById:objectId fromTable:[DataStoreService getTableName:tableName]];
}

+ (void)clearTable:(DataStoreTableType)tableName {
    [[DataStoreService createStore:tableName] clearTable:[DataStoreService getTableName:tableName]];
}

// 保存
//- (void)putTextToSpeechModel:(VDMyLocationModel *)model {
//    NSDictionary *json = [DataStoreService getObjectById:kTextToSpeechArray fromTable:Device];
//    NSMutableArray *tempArray = [NSMutableArray arrayWithArray:[NSArray yy_modelArrayWithClass:[VDMyLocationModel class] json:json]];
//    BOOL isHave = NO;
//    NSInteger index = 0;
//    for (int i = 0; i < tempArray.count; i++) {
//        VDMyLocationModel *nowModel = tempArray[i];
//        if ([nowModel.urlString isEqualToString:model.urlString]) {
//            index = i;
//            isHave = YES;
//        }
//    }
//
//    if (isHave) {
//        [tempArray removeObjectAtIndex:index];
//    }
//
//    [tempArray addObject:model];
//    NSDictionary *tempJson = [tempArray yy_modelToJSONObject];
//    [DataStoreService putObject:tempJson withId:kTextToSpeechArray intoTable:Device];
//}

@end
